fun <- expression(x^3-2*x+1)
funD <- D(fun,"x")
x <- 2
td <- 0.1
y_change <- 100
while(y_change > 0.0000001){
  y_old <- eval(fun)
  yD <- eval(funD)
  x <- x - yD * td
  y_current <- eval(fun)
  y_change <- y_old - y_current
  print(paste(x, y_current, y_change))
}